package com.example.human_resource_management.controller;

import com.example.human_resource_management.common.ResponseData;
import com.example.human_resource_management.entry.dto.PositionDepartmentDto;
import com.example.human_resource_management.entry.po.Position;
import com.example.human_resource_management.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/position")
public class PositionController {

    @Autowired
    private PositionService positionService;

    // 联查职位和部门信息
    @GetMapping("/list")
    public ResponseData<List<PositionDepartmentDto>> selectPositionDepartmentList() {
        List<PositionDepartmentDto> list = positionService.selectPositionDepartmentList();
        return ResponseData.ok(list);
    }

    // 按ID联查职位和部门信息
    @GetMapping("/{posId}")
    public ResponseData<PositionDepartmentDto> selectPositionDepartmentById(@PathVariable Long posId) {
        PositionDepartmentDto dto = positionService.selectPositionDepartmentById(posId);
        return ResponseData.ok(dto);
    }

    // 新增职位
    @PostMapping("/add")
    public ResponseData<String> addPosition(@RequestBody Position position) {
        boolean success = positionService.addPosition(position);
        return success ? ResponseData.ok("新增职位成功") : ResponseData.warn("新增职位失败");
    }

    // 修改职位
    @PutMapping("/update")
    public ResponseData<String> updatePosition(@RequestBody Position position) {
        boolean success = positionService.updatePosition(position);
        return success ? ResponseData.ok("修改职位成功") : ResponseData.warn("修改职位失败");
    }

    // 删除职位
    @DeleteMapping("/delete/{posId}")
    public ResponseData<String> deletePosition(@PathVariable Long posId) {
        boolean success = positionService.deletePosition(posId);
        return success ? ResponseData.ok("删除职位成功") : ResponseData.warn("删除职位失败");
    }
}